package flex.cc.vehicle_svr.util;

import java.nio.charset.StandardCharsets;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSONObject;

import lombok.extern.slf4j.Slf4j;


/**
 * 上游师傅坐标接口 AES 参数加密解密工具
 * @author dupenghui
 * @date 20211215
 */
@Slf4j
public class UpPointsApiAESUtils {

    public static void main(String[] args) {
        //加密
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("passWord","JFSC1357");
        jsonObject.put("channel","THIRDPARTYSYSTEM");
        String paramStr = UpPointsApiAESUtils.encryptWithHEX(jsonObject.toString(), "JFSCACSH");
        System.out.println(paramStr);

        //解密
        String result="93577E7C3217931F71D82577E5A3039915268D6CA89E71F0E609D0E2EDDC13DB03AD646CB1164617D0028F51EC283394D57D21A356AFC973DA8D0F69B5DF603E8F8F4AAE25850344B29425396673E124DC9FE16CE2EF5607A5A2AA68F5DF7BF9C1DB4BB9F54B9F54F1A41537A850AB7B0C94AFC851CF9AE65F2415B1D56850DB";
        String tokenJson = UpPointsApiAESUtils.decryptWithHEX(result, "JFSCACSH");
        System.out.println(tokenJson);
    }

    /**
     * 加密数据 生成无特殊符号加密串
     * @param secretKey 密钥
     * @param value 需加密内容
     * @author zhangyaming
     * @date 2020-04-20
     * @return 加密串
     */
    public static String encryptWithHEX(String value, String secretKey) {
        // 参数判断
        if (StringUtils.isBlank(value)) {
            return value;
        }
        value = value.trim();

        try {
            return UpPointsApiHEX.bytes2HexString(encrypt(value.getBytes(StandardCharsets.UTF_8),secretKey));
        } catch (Exception e) {
            log.info("输入值：{} 加密发生异常：{}",value,e);
            return value;
        }
    }

    /**
     * 解密无特殊符号加密串数据
     * @param secretKey 密钥
     * @param encValue 需解密值
     * @author zhangyaming
     * @date 2020-04-20
     * @return 解密结果
     */
    public static String decryptWithHEX(String encValue, String secretKey) {
        try {
            if (StringUtils.isBlank(encValue)) {
                return encValue;
            }
            encValue = encValue.trim();
            String decValue = new String(decrypt(UpPointsApiHEX.hexString2Bytes(encValue),secretKey), StandardCharsets.UTF_8);
            return StringUtils.isBlank(decValue) ? encValue : decValue;
        } catch (Exception e) {
            log.error("输入值：{} 解密发生异常：{}",encValue,e);
            return encValue;
        }
    }

    private static byte[] encrypt(byte[] data, String secretKey) throws Exception
    {

        if (StringUtils.isBlank(secretKey)) {
            throw new Exception("Please configure the ec.project.db.eccreat.aeskey env param.");
        }
        UpPointsApiAesEcb aes = new UpPointsApiAesEcb(secretKey.getBytes(StandardCharsets.UTF_8));
        return aes.encrypt(data);
    }

    private static byte[] decrypt(byte[] content, String secretKey) throws Exception
    {

        if (StringUtils.isBlank(secretKey)) {
            throw new Exception("Please configure the ec.project.db.eccreat.aeskey env param.");
        }
        UpPointsApiAesEcb aes = new UpPointsApiAesEcb(secretKey.getBytes(StandardCharsets.UTF_8));
        return aes.decrypt(content);
    }



}